package com.ciei.dpagm.service;

import com.alibaba.fastjson.JSONObject;
import com.ciei.dpagm.common.service.BaseService;
import com.ciei.dpagm.entity.GlobalConstant;
import com.ciei.dpagm.entity.Json;
import com.ciei.dpagm.mapper.GlobalConstantMapper;
import com.ciei.dpagm.util.JsonUtil;
import com.ciei.dpagm.util.UrlUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
public class GlobalConstantService extends BaseService<GlobalConstantMapper,GlobalConstant> {
	@Autowired
	private  GlobalConstantMapper globalConstantMapper;

	public GlobalConstant findByKey(String key) {
		return findById(key);
	}
	
	/**
	 * 通用的增加方法，根据手动生成主键ID进行增加的方法
	 */
	public Json addToken(String prdId) {
		
		String token = UUID.randomUUID().toString().replace("-", "");
		Json json = new Json();
		GlobalConstant tokenGC = null;
//		tokenGC = baseDAO.findById(getEntityClass(), "token" + prdId);
		tokenGC=findById("token" + prdId);
		if(tokenGC == null) {
			tokenGC = new GlobalConstant();
			tokenGC.setKeyName("token" + prdId);
			tokenGC.setValue(token);
//			baseDAO.save(tokenGC);
			save(tokenGC);
		}else {
			tokenGC.setKeyName("token" + prdId);
			tokenGC.setValue(token);
//			baseDAO.update(tokenGC);
			updateById(tokenGC);
		}
		GlobalConstant tokenTimeGC = null;
//		tokenTimeGC = baseDAO.findById(getEntityClass(), "tokenTime" + prdId);
        tokenTimeGC = findById( "tokenTime" + prdId);
		if(tokenTimeGC == null) {
			tokenTimeGC = new GlobalConstant();
			tokenTimeGC.setKeyName("tokenTime"+ prdId);
			tokenTimeGC.setValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//			baseDAO.save(tokenTimeGC);
			save(tokenTimeGC);
		}else {
			tokenTimeGC.setKeyName("tokenTime" + prdId);
			tokenTimeGC.setValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//			baseDAO.update(tokenTimeGC);
			updateById(tokenTimeGC);
		}
		//baseDAO.flushSession();很奇怪，这里不能flush
		json.setSuccess(true);
		json.setMsg("添加成功!");
		
		json.setObj(token);
		
		
		return json;
	}
	
	/**
	 * 删除多个
	 * @param deleteIds
	 * @return
	 */
	public JSONObject delete(String deleteIds){

		if(StringUtils.isBlank(deleteIds)){
			return JsonUtil.getFail("传入参数deleteIds为空");
		}

		String[] idArr = deleteIds.split(",");
		for(String id : idArr){
//			delete(globalConstant);
			globalConstantMapper.deleteById(id);
		}
		JSONObject json = new JSONObject();
		json.put("msg", "删除成功！");
		json.put("deleteIds", deleteIds);
		return json;
	}

	//获取token
	 public JSONObject GetToken() {
		 //获取appKey
		    GlobalConstant ys7AppKey = findByKey("ys7AppKey");
		    String AppKey = ys7AppKey.getValue();
		 //获取appSecret  
			GlobalConstant ys7AppSecret = findByKey("ys7AppSecret");
			String AppSecret = ys7AppSecret.getValue();
		//获取新的accessToken	
		    String Token = UrlUtil.sendPost("https://open.ys7.com/api/lapp/token/get","appKey="+AppKey+"&appSecret="+AppSecret);
		    JSONObject json = JSONObject.parseObject(Token);
		    String data = json.get("data").toString();
		    JSONObject jsontwo = JSONObject.parseObject(data);
		//把新的accessToken存到数据库    
		    String accessToken = jsontwo.get("accessToken").toString();
		    GlobalConstant ys7AccessToken = findByKey("ys7AccessToken");
		    String expireTime = jsontwo.get("expireTime").toString();
		    GlobalConstant ys7ExpireTime = findByKey("ys7ExpireTime");
		    ys7AccessToken.setValue(accessToken);
		    ys7ExpireTime.setValue(expireTime);
//		    update(ys7AccessToken, new String[] {"Value"});
//		    update(ys7ExpireTime,new String[] {"Value"});
		    updateById(ys7AccessToken);
		    updateById(ys7ExpireTime);
		//将accessToken和到期时间返回 
		    return jsontwo;
	 }
	public JSONObject add(GlobalConstant globalConstant){
		JSONObject json = new JSONObject();
		List<GlobalConstant> list = findByProperties(new String[]{"keyName"},new Object[]{globalConstant.getKeyName()});
		if(list.size()>0 && list != null) {
			json.put("msg","该键名已经存在,保存失败!");
			return json;
		}
		save(globalConstant);
		json.put("globalConstant",globalConstant);
		json.put("msg","保存成功!");
		return json;
	}
	public JSONObject update(GlobalConstant globalConstant){
		JSONObject json = new JSONObject();
		List<GlobalConstant> list = findByProperties(new String[] {"key_name"}, new Object[] {globalConstant.getKeyName()});
		if(list.size()>0 && list != null) {
			GlobalConstant g = list.get(0);
			if(!g.getKeyName().equals(globalConstant.getKeyName())) {
				json.put("msg","该键名已经存在,保存失败!");
				return json;
			}
		}else{
			json.put("msg","不可以修改键名，保存失败!");
			return json;
		}
		updateById(globalConstant);
		json.put("msg","更新成功!");
		return json;
	}

}
